package ui;
import java.util.Scanner;
import ctrl.*;
import model.*;

public class PersonUI
{
    /**
     * Instance variables
     */

    private PersonCtrl pCtrl;
    /**
     * Constructor for objects of class Person
     */
    public PersonUI()
    {
        pCtrl = new PersonCtrl();

    }

    /**
     * Her startes programmet igennem et metodekald til personMenu. 
     */
    public void Start()
    {
        personMenu();
    }

    /**
     * Denne metode holder programmet kørende i loop, indtil brugeren ønsker at lukke programmet.
     */
    private void personMenu()
    {
        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            int choice = writePersonMenu(); 
            switch(choice)
            {
                case 1:
                opretPerson();
                break;
                case 2:
                Person p = findPerson();
                if(p == null)
                {
                    System.out.println("Låner blev ikke fundet!");
                }
                else
                {
                    System.out.println(" Låners navn er:" + p.getName() +"\n");
                }           
                break;
                case 3:
                listAllPersons();
                break;
                case 4:
                updateMenu();
                break;
                case 5:
                removePerson();
                break;
                default:
                exit = true;            
            }
        }
    }

    /**
     * Personmenu til det ui, som brugeren af programmet skal se(teksten).
     */
    private int writePersonMenu()
    {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** Person Menu ***");
        System.out.println(" (1) Opret låner");
        System.out.println(" (2) Find låner");
        System.out.println(" (3) List alle lånere");
        System.out.println(" (4) Opdater låner");
        System.out.println(" (5) Fjern låner");
        System.out.println(" (6) Back");
        System.out.print("\n Vaelg: ");
        int choise;
        while(!keyboard.hasNextInt()){
            System.out.println("Indtast venligst et tal ");
            keyboard.next(); 
        }
        choise = keyboard.nextInt();
        System.out.print("\n");
        return choise;
    }

    /**
     * Brugeren angiver navn på ny person
     */
    private String inputPersonName()
    {   

        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv låners navn:  ");
        String name = keyboard.nextLine();
        return name;
    }

    /**
     * Brugeren angiver adresse på ny person
     */
    private String inputAdress()
    {   

        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv låners adresse:  ");
        String adress = keyboard.nextLine();
        return adress;
    }

    /**
     * Brugeren angiver postnummer på ny person
     */
    private String inputPostalCode()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv låners postNummer:  ");
        String postalCode = keyboard.nextLine();
        return postalCode;
    }

    /**
     * Brugeren angiver by på ny person
     */
    private String inputCity()
    {   

        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv låners city:  ");
        String city = keyboard.nextLine();
        return city;
    }

    /**
     * Brugeren angiver telefon på ny person
     */
    private String inputPhone()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv låners phoneNumber:  ");
        String phone = keyboard.nextLine();
        return phone;
    }

    /**
     * Brugeren opretter person, som kalder metoder til generering af input.
     */
    private void opretPerson()

    {
        String name = inputPersonName();
        String adress = inputAdress();
        String postalCode = inputPostalCode();
        String city = inputCity();
        String phone = inputPhone();

        pCtrl.createPerson(name, adress, postalCode, city, phone);
    }

    /**
     * Brugeren søger på personer i personarrayet på baggrund af telefonnummer
     */
    private Person findPerson()
    {
        String phone = inputPhone();
        Person p = pCtrl.findPerson(phone);
        return p;
    }

    /**
     * Denne metode opdaterer menuen
     * @return Returnerer den valgte mulighed
     */
    private int writeUpdateMenu()
    {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** Person Update Menu ***");
        System.out.println(" (1) Rediger navn");
        System.out.println(" (2) Rediger adresse");
        System.out.println(" (3) Rediger postnummer");
        System.out.println(" (4) Rediger by");
        System.out.println(" (5) Rediger telefonnummer");
        System.out.println(" (6) Back");
        System.out.print("\n Vaelg: ");
        while(!keyboard.hasNextInt()){
            System.out.println("Indtast venligst et tal ");
            keyboard.next(); 
        }
        int choise = keyboard.nextInt();
        return choise;
    }

    /**
     * Opdatere Person menu
     */
    private void updateMenu()
    {
        boolean back = false;
        Person p = findPerson();
        if(p != null)
        {
            while(!back)//that is: while exit is false
            {
                int choice = writeUpdateMenu(); 
                switch(choice)
                {
                    case 1:
                    String name = inputPersonName();
                    p.setName(name);
                    break;
                    case 2:
                    String adress = inputAdress();
                    p.setAdress(adress);
                    break;
                    case 3:
                    String postalCode = inputPostalCode();
                    p.setPostalCode(postalCode);
                    break;
                    case 4:
                    String city = inputCity();
                    p.setCity(city);
                    break;
                    case 5:
                    String phone = inputPhone();
                    p.setPhone(phone);
                    default:
                    back = true;            
                }
            }
        }
        Start();
    }          

    /**
     *  Fjerner en Person, som er fundet via Phone
     */
    private void removePerson()
    {
        String phone = inputPhone();

        if(pCtrl.removePerson(phone) == true){
            System.out.println("Låner fjernet fra lånere" + "\n");
        }
        else
        {
            System.out.println("Låner findes ikke " + "\n");
        }

    }

    /**
     * printer en liste over alle Personer
     */
    private void listAllPersons()
    {
        pCtrl.listAllPersons();
    }

}